package com.base.common.autosize

import android.content.Context
import android.os.Build
import android.util.DisplayMetrics
import androidx.annotation.RequiresApi
import com.kotlin.base.ContextProvider
import com.kotlin.base.utils.PropertiesUtil


@RequiresApi(Build.VERSION_CODES.M)
object  UIUtils {

    private val STANDARD_WIDTH= PropertiesUtil.getProperty("STANDARD_WIDTH")?.let { it.toFloat() }?:1080f

    private val STANDARD_HEIGHT= PropertiesUtil.getProperty("STANDARD_HEIGHT")?.let { it.toFloat() }?:1920f

    private  var  displayMetricsWidth=0f
    private  var displayMetricsHeight=0f
    private   var systemBarHeight=0f
    private  lateinit var displayMetrics :DisplayMetrics
    init {
        //        计算缩放系数

        //计算缩放系数


        if (displayMetricsWidth == 0.0f || displayMetricsHeight == 0.0f) {
            //在这里得到设备的真实值
//            windowManager.defaultDisplay.getMetrics(displayMetrics)
//            ContextProvider.context.display?.getMetrics(displayMetrics)


            //在这里得到设备的真实值
             displayMetrics = ContextProvider.context.resources.displayMetrics

            systemBarHeight = getSystemBarHeight( ContextProvider.context).toFloat()
            //横屏
            if (displayMetrics.widthPixels > displayMetrics.heightPixels) {
                this.displayMetricsWidth = displayMetrics.heightPixels.toFloat()
                displayMetricsHeight = (displayMetrics.widthPixels - systemBarHeight) as Float
            } else {
               //竖屏
                this.displayMetricsWidth = displayMetrics.widthPixels.toFloat()
                displayMetricsHeight = (displayMetrics.heightPixels - systemBarHeight) as Float
            }

        }
    }

    fun getHorizontalScaleValue(): Float {
        return displayMetricsWidth / STANDARD_WIDTH
    }

    fun getVerticalScaleValue(): Float {
        return displayMetricsHeight / (STANDARD_HEIGHT - systemBarHeight)
    }

    fun getSystemBarHeight(context: Context): Int {
        return getValue(context, "com.android.internal.R\$dimen", "system_bar_height", 48)
    }

    fun getWidth(width: Int): Int {
        return Math.round(width.toFloat() * displayMetricsWidth / STANDARD_WIDTH)
    }

    fun getHeight(height: Int): Int {
        return Math.round(height.toFloat() * displayMetricsHeight / (STANDARD_HEIGHT - systemBarHeight))
    }


    private fun getValue(
        context: Context,
        dimeClass: String,
        system_bar_height: String,
        defaultValue: Int
    ): Int {
//        com.android.internal.R$dimen    system_bar_height   状态栏的高度
        var defaultValue = defaultValue
        try {

            //获取状态栏高度的资源id
            val resourceId =
                context.resources.getIdentifier("status_bar_height", "dimen", "android")
            if (resourceId > 0) {
                defaultValue = context.resources.getDimensionPixelSize(resourceId)
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return defaultValue
    }

}